package org.example.maven.aspect;

import jakarta.servlet.http.HttpServletRequest;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.example.maven.mapper.collectionMapper;
import org.example.maven.mapper.provenanceSourceMapper;
import org.example.maven.pojo.ProvenanceSource;
import org.example.maven.pojo.Result;
import org.example.maven.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Aspect
@Component
public class UploadExcelFileAspect {
    @Autowired
    private HttpServletRequest request;  // 获取请求信息
    @Autowired
    private collectionMapper collectionMapper;

    @Pointcut("execution(* org.example.maven.controller.utilController.uploadCollectionFile(..))")
    private void uploadExcelPointCut(){}

    @AfterReturning(value = "uploadExcelPointCut()", returning = "result")
    private void AfterUploadExcel(JoinPoint joinPoint,Object result){
        Result res=(Result) result;
        List<ProvenanceSource>list= (List<ProvenanceSource>) res.get("data");
        List<Integer> listId=new ArrayList<>();
        for(ProvenanceSource p:list){
            listId.add(p.getCollectionId());
        }
        System.out.println(list);
        collectionMapper.addReviewCollectionList(listId,getUserId());
    }
    private Integer getUserId(){
        String token=request.getHeader("Authorization");
        return JwtUtils.getJwtUserId(token);
    }
}
